﻿using DotNetStar.TheKing.Domain;
using DotNetStar.TheKing.Services;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DotNetStar.TheKing.Web
{
    public class DetailAskViewComponent:ViewComponent
    {
        private readonly ContentService _contentService;
        public DetailAskViewComponent(ContentService contentService)
        {
            _contentService = contentService;
        }

        public async Task<IViewComponentResult> InvokeAsync(Content content, IEnumerable<ContentComment> comments)
        {
            ViewData["Comments"] = comments;

            //等待回答的列表
            ViewData["NotAnwserList"]= _contentService.GetTopContentQueable(new ContentQuery { Channel = content.Channel })
                .Where(m=>!m.BestCommentId.HasValue)
                .OrderByDescending(m => m.PostedTime)
                .ToList();

            //TA的问题
            ViewData["UserContents"] = _contentService.GetTopContentQueable(new ContentQuery { Channel = content.Channel })
                .Where(m => m.Author.Id == content.AuthorId && m.Id != content.Id).OrderByDescending(m => m.PostedTime).ToList();


            if (content.BestCommentId.HasValue)
            {
                //最佳答案
                ViewData["BestAnswer"] = comments.SingleOrDefault(m => m.Id == content.BestCommentId.Value);
            }

            return View(content);
        }
    }
}
